Right Shift Operator (>>) with Signed Integers

When using the bitwise right shift (>>) operator with signed integers, it’s essential to understand how the sign bit is preserved and the implications of this preservation. Let’s explore this with examples:

Preserving Sign Bit:

In signed integers, the leftmost bit (most significant bit) represents the sign of the number. When right-shifting signed integers, the sign bit is preserved, meaning it is replicated to fill the vacant leftmost positions.

Example:

# Signed right shift
x = -8 # Binary: 1111 1000 (assuming 8-bit signed integer)
y = x >> 1 # Binary: 1111 1100, Decimal: -4

In this example, when -8 is right-shifted by 1 position, the sign bit (1) is replicated, resulting in 1111 1100, which represents -4 in decimal.

Use Case: Division by Powers of 2 with Signed Integers

C++
#include <iostream>

int main() {
    int num = -16;
    int divisor = 4;
    int result = num >> 2; // Equivalent to num / 2^2
    std::cout << result << std::endl; // Output: -4
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int num = -16;
        int divisor = 4;
        int result = num >> 2; // Equivalent to num / 2^2
        System.out.println(result); // Output: -4
    }
}
Python
# Division by powers of 2 using right shift with signed integers
num = -16
divisor = 4
result = num >> 2 # Equivalent to num // 2**2
print(result) # Output: -4
C#
using System;

class Program
{
    static void Main(string[] args)
    {
        int num = -16;
        int result = num >> 2; // Equivalent to num / 2^2
        Console.WriteLine(result); // Output: -4
    }
}
//This code is contributed by Adarsh.
Javascript
// JavaScript pprogram 

// Main function
function main() {
    let num = -16;
    let result = num >> 2; // Equivalent to num / 2^2
    console.log(result);  
}

// Call the main function to execute the program
main();

Output
-4

In this case, -16 is right-shifted by 2 positions, which is equivalent to dividing -16 by 2**2. The sign bit is preserved during the right shift, resulting in -4.

Right Shift Operator (>>) in Programming

Right shift operator (>>), commonly found in programming languages, including C, C++, Java, and others, is used to shift the bits of a number to the right by a specified number of positions. Each shift moves all bits in the operand to the right by the number of positions indicated by the right operand.

Table of Content

  • Right Shift Operator (>>) Definition
  • Right Shift Operator (>>) Syntax
  • Right Shift Operator (>>) Examples
  • Right Shift Operator (>>) with Signed Integers
  • Right Shift Operator (>>) with Unsigned Integers
  • Right Shift Operator (>>) with Signed vs. Unsigned Integers
  • Logical Right Shift
  • Arithmetic Right Shift
  • Logical Right Shift vs. Arithmetic Right Shift
  • Right Shift Operator (>>) Optimization Techniques
  • Bit Manipulation Hacks with Right Shift Operator

This comprehensive guide aims to provide a deep understanding of bitwise right shift operators, from the foundational principles to advanced optimization strategies.

Similar Reads

Right Shift Operator (>>) Definition:

When you right-shift a binary number by n positions, each bit in the number is moved n positions to the right. This effectively divides the number by 2^n (equivalent to integer division by 2^n). The rightmost n bits are discarded, and 0 bits are shifted in from the left....

Right Shift Operator (>>) Syntax:

The syntax of the bitwise right shift operator is simple and consistent across programming languages:...

Right Shift Operator (>>) Examples:

Consider the binary number 1101 0010, which is 210 in decimal. If we right-shift it by 2 positions:...

Right Shift Operator (>>) with Signed Integers:

When using the bitwise right shift (>>) operator with signed integers, it’s essential to understand how the sign bit is preserved and the implications of this preservation. Let’s explore this with examples:...

Right Shift Operator (>>) with Unsigned Integers:

When using the bitwise right shift (>>) operator with unsigned integers, the behavior is simpler compared to signed integers. Let’s explore how the right shift operator works with unsigned integers:...

Right Shift Operator (>>) with Signed vs. Unsigned Integers:

AspectSigned IntegersUnsigned IntegersSign Bit PreservationSign bit is preserved, replicated to fill vacant leftmost positions.No sign bit, all bits are shifted to the right, filling vacant positions with zeros.Example-8 >> 1 results in -4 (Binary: 1111 1100)8 >> 1 results in 4 (Binary: 0000 0100)Division by Powers of 2Right shift performs signed division by 2^n.Right shift performs unsigned division by 2^n.Implementation in ProgrammingBehavior depends on language and compiler. Most use arithmetic right shift.Always performs logical right shift....

Logical Right Shift:

Bitwise logical right shift refers to the operation of shifting the bits of a binary number to the right by a specified number of positions while filling the vacant leftmost positions with zeros. This operation is commonly used with unsigned integers and is denoted by the >> operator in most programming languages....

Arithmetic Right Shift:

Bitwise arithmetic right shift is an operation used to shift the bits of a binary number to the right by a specified number of positions while preserving the sign of the number. This operation is commonly used with signed integers and is denoted by the >> operator in many programming languages....

Logical Right Shift vs. Arithmetic Right Shift:

Logical right shift: In logical right shift, vacant leftmost positions are filled with zeros. It’s commonly used for unsigned integers.Arithmetic right shift: In arithmetic right shift, vacant leftmost positions are filled with the sign bit. It’s used for signed integers to preserve the sign of the number....

Right Shift Operator (>>) Optimization Techniques:

1. Division by Powers of 2:...

Bit Manipulation Hacks with Right Shift Operator:

Bitwise right shift (>>) is a fundamental operation in bit manipulation and bit twiddling hacks. It is often used in combination with other bitwise operators to achieve various tasks efficiently. Here are some common bit manipulation and bit twiddling hacks that involve bitwise right shift:...

Contact Us